{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Moments: Mean, Variance, Skew, Kurtosis"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Create a roughly normal-distributed random set of data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD8CAYAAAB5Pm/hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAERRJREFUeJzt3W2MnNdZxvH/VacNqFQkIa5rbAcbySo4iL5oZaq+Ka1p\nYzVVHT5gGQkw1JJVKaVFQqocKsEnSy5IiCIRkJUWjFrqWkCIRdIGxxBVfGhTpzhN7DTEbRzZlt+a\nvlGQXOzefNgn6cT17sx4Z3Z2j/8/aTVnzpxn5/axfc3ZM888m6pCktSul026AEnSeBn0ktQ4g16S\nGmfQS1LjDHpJapxBL0mNM+glqXEGvSQ1zqCXpMZdN+kCAG6++eZavXr1pMuQpEXlscce+2ZVLe03\nbqCgT3IDcC/wS0AB7weeBj4LrAaOA5ur6tvd+LuBbcAl4ENV9dBs33/16tUcOnRokFIkSZ0kzw0y\nbtCtm48Dn6+qXwBeBzwF7AAOVtVa4GB3nyTrgC3ArcBG4J4kS4YrX5I0Kn2DPslPA28HPgFQVT+o\nqu8Am4A93bA9wJ1dexOwt6ouVNWzwDFg/agLlyQNZpAV/RrgPPA3Sf4zyb1JXgksq6rT3ZgzwLKu\nvQI40XP8ya5PkjQBgwT9dcAbgb+qqjcA/0O3TfOCmr7W8VDXO06yPcmhJIfOnz8/zKGSpCEMEvQn\ngZNV9aXu/j8wHfxnkywH6G7PdY+fAlb1HL+y63uJqtpdVVNVNbV0ad83jSVJV6lv0FfVGeBEktd2\nXRuAo8B+YGvXtxW4v2vv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[
       "<matplotlib.figure.Figure at 0x24c165132b0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "vals = np.random.normal(0, 0.5, 10000)\n",
    "\n",
    "plt.hist(vals, 50)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "The first moment is the mean; this data should average out to about 0:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0030761613438204053"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(vals)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "The second moment is the variance:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.24962106559173047"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.var(vals)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "The third moment is skew - since our data is nicely centered around 0, it should be almost 0:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.018249260513551597"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import scipy.stats as sp\n",
    "sp.skew(vals)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "The fourth moment is \"kurtosis\", which describes the shape of the tail. For a normal distribution, this is 0:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.04795034855803815"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.kurtosis(vals)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Activity"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Understanding skew: change the normal distribution to be centered around 10 instead of 0, and see what effect that has on the moments.\n",
    "\n",
    "The skew is still near zero; skew is associated with the shape of the distribution, not its actual offset in X."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
